package com.tansun.easycare.rule.ruleexcute.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.common.model.SelectModelVo;
import com.tansun.easycare.rule.ruleexcute.model.RuleExecuteVO;
import com.tansun.easycare.rule.ruleexcute.service.RuleExcuteService;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.rule.common.RuleModelTypeEnum;

/**
 * 规则计算 提供规则项目基于流的计算
 *
 * @author lixs
 */
@RestController
@RequestMapping(value = "/ruleProj/calculationEngine")
public class RuleExcuteController extends RuleBaseController {


    @Autowired
    private RuleExcuteService ruleExcuteService;
    
    @Autowired
    private  RuleManageService   ruleManageService;



    /**
     * 获取待测试的xml 获取json格式的文本数据
     *
     * @param type xml 或者 json
     * @param map  ModelMap
     * @return xml 或者 json的数据模型的文本数据
     */
    @RequestMapping("ruleExcuteTypeChange")
    public 
    ResponseVO ruleExcuteTypeChange(@RequestBody RuleExecuteVO ruleExecute) throws Exception {
        String json = ruleExcuteService.getRuleFlowXmlForTest(ruleExecute);
        ResponseVO  res  = new  ResponseVO();
        res.setData(json);
        return res;
    }
    
    /**
     * 规则计算
     *
     * @param request      HttpServletRequest
     * @param map          ModelMap
     * @param type         文本类型 xml json 两种
     * @param inputXml     输入的xml文本
     * @param ruleflowInfo 规则流
     * @param target       计算目标 衍生计算还是规则执行
     * @return 返回规则计算结果
     */
    @RequestMapping("ruleExcute")
    public ResponseVO
     doRule( @RequestBody RuleExecuteVO ruleExecuteVO){
        ResponseVO res  = new  ResponseVO();
        String resultxml = "";
		try {
		    resultxml = this.ruleExcuteService.excuteDrl(ruleExecuteVO);
			res.setData(resultxml);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			Throwable root = ExceptionUtils.getRootCause(e);
			if( root==null ) {
				root = e;
			}
			resultxml = "计算失败：" + root.getMessage();
            String desDetail = com.tansun.easycare.core.utils.StringUtils.toHtml(org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e));
            res.setDescription("计算失败："+e.getMessage());
			res.setStatus(ResponseVO.ERROR);
			res.setData(resultxml);
			return res;
		}
        return res;
    } 

    
    @RequestMapping("trackList")
    @SuppressWarnings("unchecked")
    public   ResponseVO   trackList(@RequestBody Map<String,Object> param){
    	  List<SelectModelVo>  retList  = new  ArrayList<>();
    	  ResponseVO  resVo = new ResponseVO(); 
    	  resVo.setData(retList);
    	  try {
	    	  String  projectCode  = this.getCurrProjectCode();
	    	  if (param != null) {
	    		    SelectModelVo  selectModelVo = null;
	    		    for (Entry<String, Object> enty:  param.entrySet()) {
	    		        String  key =	enty.getKey();
	    		        Object   obj  = enty.getValue();
	    		        selectModelVo  = new  SelectModelVo();
	    		        RuleManage  ruleManage = this.ruleManageService.findByCode(key, projectCode);
	    		        selectModelVo.setName(ruleManage.getName());
	    		        selectModelVo.setCode(RuleModelTypeEnum.getName(ruleManage.getModelType()));
	    		        retList.add(selectModelVo);
	    		        if (RuleModelTypeEnum.PolicyRuleGroup.type.equals(key.substring(0, 3))) {
	    		        	  if (obj instanceof ArrayList) {
								List<String>  tmList  =   (List<String>) obj; 
	    		        		 for (String  str:tmList) {
	    		        			 selectModelVo = new SelectModelVo();
	    		        			 selectModelVo.setName("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+str);
	    		        			 selectModelVo.setCode("");
	    		        			 retList.add(selectModelVo);
	    		        		 }
	    		        	  }
	    		        }
	    		    }
	    	  }
    	  }catch(Exception e) {
    		  resVo.setStatus(ResponseVO.ERROR);
    	  }
    	  return  resVo;
    }
}
